<template>
  <div class="edu-page-warp">
    <condition-card :showResetBtn="true" @search="searchList" @reset="reset">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('tutorAuth.entranceGrade')">
              <!-- <GradeSelect v-model="queryParams.grdade" :showAll="false"></GradeSelect> -->
              <el-date-picker
                style="width:100%"
                v-model="queryParams.grdade"
                align="right"
                type="year"
                value-format="yyyy"
                placeholder="请选择"
                :clearable="true">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('tutorAuth.currentGrade')">
              <!-- <GradeSelect v-model="queryParams.currentGrade" :showAll="false"></GradeSelect> -->
              <el-date-picker
                style="width:100%"
                v-model="queryParams.currentGrade"
                align="right"
                type="year"
                value-format="yyyy"
                placeholder="请选择"
                :clearable="true">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
    </condition-card>
    <el-card shadow="never">
      <div class="edu-page-title">{{$t('tutorAuth.authManageList')}}</div>
      <div class="edu-opertion-bar">
        <div class="float-right">
          <el-button @click="updateRecord(null)" v-premit="add_tutorAuth" type="primary">{{$t('common.btn.add')}}</el-button>
        </div>
      </div>
      <edu-table url="/api/welcomeservice/tutorApplicationAuthorith/list" ref="mainTable" :order="true" :selection="false">
        <el-table-column :label="$t('tutorAuth.entranceGrade')" prop="grdade"/>
        <el-table-column :label="$t('tutorAuth.currentGrade')" prop="currentGrade"/>
        <el-table-column :label="$t('label.college')" prop="facultyI18n" v-if="tutorType == 2" show-overflow-tooltip>
          <template slot-scope="scope">
              <span v-if="scope.row.facultyI18n">{{scope.row.facultyI18n}}</span>
              <span v-else>全部</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('label.major')" v-if="tutorType == 2" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.major">{{scope.row.majorI18n}}</span>
            <span v-else>全部</span>
          </template>
        </el-table-column>
        <!-- <el-table-column :label="$t('label.major')" prop="majorI18n" v-if="tutorType == 2 && major" show-overflow-tooltip/>
        <el-table-column :label="$t('label.major')" prop="majorI18n" v-if="tutorType == 2 && major" show-overflow-tooltip/> -->
        <el-table-column :label="$t('tutorAuth.applyTime')" show-overflow-tooltip>
          <template slot-scope="scope">
            {{transTime(scope.row)}}
          </template>
        </el-table-column>
        <el-table-column :label="$t('tutorAuth.isReselection')" prop="isReelection">
          <template slot-scope="scope">
           <el-tag v-if="scope.row.isReelection == 1 ">{{$t('message.yes')}}</el-tag>
           <el-tag v-else >{{$t('message.no')}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('message.operating')" fixed="right">
          <template slot-scope="scope">
            <el-button type="text"  @click="updateRecord(scope.row)" v-premit="'edit_tutorAuth'" size="mini" >{{$t('message.edit')}}</el-button>
            <el-button type="text"  @click="deletelRecord(scope.row)" v-premit="'delete_tutorAuth'" size="mini" >{{$t('message.delete')}}</el-button>
          </template>
        </el-table-column>
      </edu-table>
    </el-card>

    <edu-dialog :title="dialogTitle" width="600px" ref="updateDialog" :auto-height="true" @close="resetAddForm" >
      <el-form :model="createForm" ref="createForm" label-width="100px" label-suffix=":" >
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('tutorAuth.entranceGrade')" prop="grdade"
              :rules="[
                  { required: true, message: $t('message.inputRequired'), trigger: 'blur' }
                ]"
            >
            <GradeSelect v-model="createForm.grdade" :showAll="false"></GradeSelect>
            <!-- <el-date-picker
                style="width:100%"
                v-model="createForm.grdade"
                align="right"
                type="year"
                value-format="yyyy"
                placeholder="请选择"
                :clearable="true">
              </el-date-picker> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('tutorAuth.currentGrade')" prop="currentGrade"
              :rules="[
                  { required: true, message: $t('message.inputRequired'), trigger: 'blur' }
                ]"
            >
              <GradeSelect v-model="createForm.currentGrade" :showAll="false"></GradeSelect>
              <!-- <el-date-picker
                style="width:100%"
                v-model="createForm.currentGrade"
                align="right"
                type="year"
                value-format="yyyy"
                placeholder="请选择"
                :clearable="true">
              </el-date-picker> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('label.college')" prop="faculty" v-if="tutorType == 2"
            >
              <DictSelect v-model="createForm.faculty" :data="options.faculty"></DictSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('label.major')" prop="major" v-if="tutorType == 2"
              
            >
            <!-- :rules="[
                  { required: true, message: $t('message.inputRequired'), trigger: 'blur' }
                ]" -->
              <!-- <MajorSelect :faculty="createForm.faculty" v-model="createForm.major"></MajorSelect> -->
              <MyMajor
              :faculty="createForm.faculty"
              :grade="createForm.currentGrade"
              v-model="createForm.major"
            ></MyMajor>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('message.startTime')" prop="startTime"
              :rules="[
                  { required: true, message: $t('message.inputRequired'), trigger: 'blur' }
                ]"
            >
              <el-date-picker type="datetime" :placeholder="$t('message.startDate')" 
                v-model="createForm.startTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="width:100%;hegiht:100:"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('message.endTime')" >
              <el-date-picker type="datetime" :placeholder="$t('message.startDate')"
                v-model="createForm.endTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="width:100%;hegiht:100:"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('tutorAuth.isReselection')" prop="isReelection"
              :rules="[
                  { required: true, message: $t('message.inputRequired'), trigger: 'blur' }
                ]"
            >
            <DictSelect v-model="createForm.isReelection" :data="options.isReelection"></DictSelect>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="buttons">
        <el-button @click="close(createForm)">{{$t('message.cancel')}}</el-button>
        <el-button @click="submitForm" type="primary">{{$t('message.submit')}}</el-button>
      </span>
    </edu-dialog>
    <el-dialog
        title="系统提示"
        :visible.sync="dialogVisible"
        width="400px"
        >
        <span>返回将不会保存当前信息激活设置，是否确定？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancle">取 消</el-button>
          <el-button type="primary" @click="summit">确 定</el-button>
          <el-button type="primary" @click="saveClose">保存并关闭</el-button>
        </span>
    </el-dialog> 
  </div>
</template>
<script>
import { queryDic } from "common/src/api/dictionary";
import APIs from "common/src/api/welcomeService/schoolRelation"
import MyMajor from "./MyMajor";
import UTILS from "common/src/utils/utils";

const defaultCreateForm = {
  grdade: null,
  currentGrade: null,
  faculty: null,
  major: null,
  startTime: null,
  endTime: null,
  isReelection:0,
  type:""
};
// 本科生选导师开放时间
export default {
  name: "TutorApplyAuth",
  props: ["tutorType"],
  components: { MyMajor },
  computed: {},
  mounted() {
    this.getOptions();
  },
  data() {
    return {
      dialogTitle: "",
      dialogVisible:false,
      options: {
        faculty: [],
        isReelection:[{label:this.$t('message.no'),value:0},{label:this.$t('message.yes'),value:1}]
      },
      queryParams: {
        grdade: null,
        currentGrade: null,
        type:""
      },
      createForm: UTILS.clone(defaultCreateForm)
    };
  },
  methods: {
    getOptions() {
      //筛选条件字典查询
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: [
          "X_YX"
        ]
      };
      queryDic(data).then(res => {
        UTILS.fillSelect(
            {
              faculty: "X_YX"
            },
            res.data,
            this.options
        );
      });
      this.searchList();
    },
    close(data){
      if(data.id){//编辑
        this.closeDialog();
      }else{
         this.dialogVisible=true;
      }
    },
    cancle(){
      this.dialogVisible=false;
    },
    summit(){
      this.dialogVisible=false;
      this.closeDialog();
    },
    saveClose(){
      this.dialogVisible=false;
      this.submitForm();
    },
    closeDialog(){
      this.$refs.updateDialog.close();
      this.$refs.createForm.clearValidate();
      this.searchList();
    },
    deletelRecord(data) {
        let ids=[];
        ids.push(data.id)
        this.createForm.ids=ids;
        this.$delConfirm().then(() => {
          APIs.deleteTutorAuth(this.createForm).then(res =>{
          if(res.code=200){
                this.$message({
                type: "success",
                message: this.$t("common.tips.deleteSuccess")
              });
              this.searchList();
            }
          }) 
        })     
    },
    updateRecord(data) {
      if(!data){//新增
          this.dialogTitle=this.$t("message.add");
          this.createForm = UTILS.clone(defaultCreateForm);
      }else{
          this.dialogTitle=this.$t("message.edit");
          this.createForm =  _.cloneDeep(data)
          let that=this;
          setTimeout(() => {
              that.createForm.major=data.major;
          }, 500);
      }
      this.$refs.updateDialog.open();
    },
    reset(){
       for (const key in this.queryParams) {
          this.queryParams[key] = ''  
        }
        this.searchList();
    },
    searchList() {
      this.queryParams.type = this.tutorType;
       this.$refs.mainTable.search(this.queryParams);
    },
    resetAddForm() {
      this.$refs.createForm.clearValidate();
      this.searchList();
    },
    submitForm() {
      this.$refs.createForm.validate(valid => {
        if (valid) {
          let item = UTILS.clone(this.createForm);
          item.type=this.tutorType;
          if(item.faculty==''){
            item.faculty=null
          }
          if(item.major==''){
            item.major=null
          }
          //item.endTime =item.endTime.substring(0,10)+' 23:59:59'
          if(!this.compareTime(item)){
            this.$message({
              type: "warning",
              message: this.$t('tutorAuth.timeRule')
            });
            return;
          }
          if(!this.compareGrade(item)){
              this.$message({
              type: "warning",
              message: '当前年级必须大于等于入学年级'
            });
            return;
          }
          if (item.id) {//编辑
            APIs.editTutorAuth(item).then(response => {
              if (response.code == 200) {
                this.$message({
                  type: "success",
                  message: this.$t("message.editSuccess")
                });
                this.closeDialog();
              }
            });
          } else {//新增
            APIs.addTutorAuth(item).then(response => {
              if (response.code == 200) {
                this.$message({
                  type: "success",
                  message: this.$t("message.addSuccess")
                });
                this.closeDialog();
              }
            });
          }
        }
      });
    },
    transTime(item){
      if(!item.endTime){
        //return moment(item.startTime).format('YYYY/MM/DD')+"--"+this.$t('tutorAuth.forEver');
        return item.startTime+"--"+this.$t('tutorAuth.forEver');
      }
      //return moment(item.startTime).format('YYYY/MM/DD')+"--"+moment(item.endTime).format('YYYY/MM/DD');
      return item.startTime+"--"+item.endTime;
    },
    compareTime(data){
        if(data.endTime){
            var end_str = (data.endTime).replace(/-/g,"/");
            var start_str = (data.startTime).replace(/-/g,"/");
          return  new Date(end_str) > new Date(start_str)
        }else{
          return true;
        }
    },
    compareGrade(data){
      return data.currentGrade >= data.grdade;
    }
  }
};
</script>